<?php
/**
 *
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
if( !defined( "IN_SCRIPT" ) ) {
    die();
}

class sessionWrapper {
    function sessionWrapper() {
        session_start();
    }

    function login( $user, $password, $use_cookie ) {
        $this->logout();
        session_start();
        global $CONFIG;

        if( $user == $CONFIG['username'] && md5($password) == $CONFIG['userpass'] ) {
            $_SESSION["login_username"] = $CONFIG['username'];
            $_SESSION["login_pwd"] = md5( $CONFIG['userpass'] );
            if( $use_cookie ) {
                $cookiedata = md5( $CONFIG['username'] . "-" . $CONFIG['userpass'] );
                setcookie( 'avp_login', $cookiedata, time() + 2592000 );
            }
            return true;
        } else {
            return false;
        }
    }

    function logout() {
        session_unset();
        session_destroy();
        setcookie( 'avp_login', false, 0 );
    }

    function check_login() {
        global $CONFIG;
        if( isset( $_SESSION["login_username"] ) && isset( $_SESSION["login_pwd"] ) && $_SESSION["login_username"] == $CONFIG['username'] && $_SESSION["login_pwd"] == md5( $CONFIG['userpass'] ) ) {
            return true;
        } elseif( isset( $_COOKIE["avp_login"] ) ) {
            if( $_COOKIE["avp_login"] == md5( $CONFIG['username'] . "-" . $CONFIG['userpass'] ) ) {
                // Sync session to cookie
                $_SESSION['login_username'] = $CONFIG['username'];
                $_SESSION['login_pwd'] = md5( $CONFIG['userpass'] );
                return true;
            }
        } else {
            return false;
        }
    }
}

?>